﻿Imports System.Data.Linq
Imports System.Linq

Public Class CHBLController
    Dim objDataContext As New TanNamChinhDataContext
    Public Function SelectAllRecord() As IQueryable(Of tblHBL)
        Try
            Return objDataContext.tblHBLs
        Catch ex As Exception
            Return Nothing
        End Try
    End Function
    Public Function SelectRecordByID(ByVal _id As Integer) As tblHBL
        Try
            Return (From r In objDataContext.tblHBLs
                    Where r.ID_HBL = _id
                    Select r).Single
        Catch ex As Exception
            Return Nothing
        End Try
    End Function
    Public Function SelectAllRecordByFile(ByVal idFile As Integer) As IQueryable(Of tblHBL)
        Try
            Return (From h In objDataContext.tblHBLs
            Where h.tblFileMBL.ID_File = idFile
            Select h
                )
        Catch ex As Exception
            Return Nothing
        End Try
    End Function
    Public Function InsertRecord(ByVal LocalTable As tblHBL) As Integer
        Try
            objDataContext.tblHBLs.InsertOnSubmit(LocalTable)
            objDataContext.SubmitChanges()
            Return LocalTable.ID_HBL
        Catch ex As Exception
            Throw ex
        End Try
    End Function
    Public Sub UpdateRecord(ByVal LocalTable As tblHBL)
        Try
            objDataContext.tblHBLs.Attach(LocalTable)
            objDataContext.Refresh(RefreshMode.KeepCurrentValues, LocalTable)
            objDataContext.SubmitChanges(ConflictMode.ContinueOnConflict)
        Catch ex As Exception
            Throw ex
        End Try
    End Sub
    Public Sub UpdateRecordWithoutAttach(ByVal LocalTable As tblHBL)
        Try
            objDataContext.Refresh(RefreshMode.KeepCurrentValues, LocalTable)
            objDataContext.SubmitChanges(ConflictMode.ContinueOnConflict)
        Catch ex As Exception
            Throw ex
        End Try
    End Sub

    Public Sub DeleteRecord(ByVal _id As Integer)
        Try
            objDataContext.tblHBLs.DeleteOnSubmit( _
              (From r In objDataContext.tblHBLs _
               Where r.ID_HBL = _id).Single)
            objDataContext.SubmitChanges()
        Catch ex As Exception
            Throw ex
        End Try
    End Sub
    Public Sub New()
        objDataContext = New TanNamChinhDataContext
    End Sub
    Public Sub New(ByVal _obj As TanNamChinhDataContext)
        objDataContext = _obj
    End Sub
    Public Sub RefreshData()
        objDataContext = New TanNamChinhDataContext
    End Sub
End Class
